home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / ASM-PPC / SEMAPHOR.{1I < prev    next >
Text File  |  1999-09-17  |  1KB  |  69 lines

  1. #ifndef _PPC_SEMAPHORE_H
  2. #define _PPC_SEMAPHORE_H
  3.  
  4. /*
  5.  * SMP- and interrupt-safe semaphores..
  6.  *
  7.  * (C) Copyright 1996 Linus Torvalds
  8.  * Adapted for PowerPC by Gary Thomas and Paul Mackerras
  9.  */
  10.  
  11. #include <asm/atomic.h>
  12.  
  13. struct semaphore {
  14.     atomic_t count;
  15.     atomic_t waking;
  16.     struct wait_queue *wait;
  17. };
  18.  
  19. #define sema_init(sem, val)    atomic_set(&((sem)->count), (val))
  20.  
  21. #define MUTEX        ((struct semaphore) \
  22.              { ATOMIC_INIT(1), ATOMIC_INIT(0), NULL })
  23. #define MUTEX_LOCKED    ((struct semaphore) \
  24.              { ATOMIC_INIT(0), ATOMIC_INIT(0), NULL })
  25.  
  26. extern void __down(struct semaphore * sem);
  27. extern int  __down_interruptible(struct semaphore * sem);
  28. extern int  __down_trylock(struct semaphore * sem);
  29. extern void __up(struct semaphore * sem);
  30.  
  31. extern inline void down(struct semaphore * sem)
  32. {
  33.     if (atomic_dec_return(&sem->count) >= 0)
  34.         wmb();
  35.     else
  36.         __down(sem);
  37. }
  38.  
  39. extern inline int down_interruptible(struct semaphore * sem)
  40. {
  41.     int ret = 0;
  42.  
  43.     if (atomic_dec_return(&sem->count) >= 0)
  44.         wmb();
  45.     else
  46.         ret = __down_interruptible(sem);
  47.     return ret;
  48. }
  49.  
  50. extern inline int down_trylock(struct semaphore * sem)
  51. {
  52.     int ret = 0;
  53.  
  54.     if (atomic_dec_return(&sem->count) >= 0)
  55.         wmb();
  56.     else
  57.         ret = __down_trylock(sem);
  58.     return ret;
  59. }
  60.  
  61. extern inline void up(struct semaphore * sem)
  62. {
  63.     mb();
  64.     if (atomic_inc_return(&sem->count) <= 0)
  65.         __up(sem);
  66. }    
  67.  
  68. #endif /* !(_PPC_SEMAPHORE_H) */
  69.